﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseInterface;
using BaseUnits;
using System.Data.SqlClient;

namespace Formulas
{
    public class PriorityReport
    {
        Connection cnx;
        public PriorityReport(Connection cnx)
        {
            this.cnx = cnx;
        }
        public ReportData.PriorityDataTable GetPriority(DateTime date, Grid grid)
        {
            ReportData.PriorityDataTable table = new ReportData.PriorityDataTable();
            int priority;
            foreach (IPowerPlant p in grid.PowerPlants)
            {
                foreach (IPowerUnit pu in p.PowerUnits)
                {
                    priority = pu.Priority(date);
                    table.AddPriorityRow(priority,pu.Description,pu.IsCDM,pu.ID,date);
                }
            }
            return table;
        }
        public void UpdatePriority(ReportData.PriorityDataTable table)
        {
            SqlDataAdapter da = new SqlDataAdapter();
            SqlCommand InsertCmd = new SqlCommand("INSERT INTO PowerUnitPriority (PowerUnitId,Priority,Date,Hour) VALUES (@PowerUnitId,@Priority,@Date,1)", this.cnx.getConnection());
            InsertCmd.Parameters.Add("@PowerUnitId", System.Data.SqlDbType.Int, 4, "PowerUnitId");
            InsertCmd.Parameters.Add("@Priority", System.Data.SqlDbType.Int, 4, "Priority");
            InsertCmd.Parameters.Add("@Date", System.Data.SqlDbType.Date, 3, "Date");

            SqlCommand UpdateCmd = new SqlCommand("UPDATE PowerUnitPriority SET Priority = @Priority, Date = @Date, Hour = 1 WHERE PowerUnitId = @PowerUnitId", this.cnx.getConnection());
            UpdateCmd.Parameters.Add("@PowerUnitId", System.Data.SqlDbType.Int, 4, "PowerUnitId");
            UpdateCmd.Parameters.Add("@Priority", System.Data.SqlDbType.Int, 4, "Priority");
            UpdateCmd.Parameters.Add("@Date", System.Data.SqlDbType.Date, 3, "Date");

            da.InsertCommand = InsertCmd;
            da.UpdateCommand = UpdateCmd;
            da.Update(table);
        }
    }
}
